package com.sh1604.duitang.fragment;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sh1604.duitang.R;
import com.sh1604.duitang.activity.HomeItemClickActivity;
import com.sh1604.duitang.adapter.HomePagerAdapter;
import com.sh1604.duitang.constant.OKHttpUtils;
import com.sh1604.duitang.constant.UrlString;
import com.sh1604.duitang.mybean.Bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private ListView lv_home;
    private Toolbar toolbar;
    private List<Bean.DataBean.ObjectListBean> mData;
    private ArrayList<String> mUrlList;
    private Activity mActivity;
    private HomePagerAdapter adapter;

    public HomeFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mData = new ArrayList<>();
        //单项点击的url集合
        mUrlList = new ArrayList<>();
        mActivity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lv_home = (ListView)view.findViewById(R.id.lv_home);
        initToolBar(view);
        loadData();
        adapter = new HomePagerAdapter(getContext(), mData);
        lv_home.setAdapter(adapter);
        //对listview进行监听
        setListener(lv_home);
    }

    private void setListener(ListView lv_home) {
        lv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //把url集合以及位置传到跳转页面
                Intent intent = new Intent(mActivity, HomeItemClickActivity.class);
                intent.putStringArrayListExtra("url",mUrlList);
                intent.putExtra("position",position);
                Bundle bundle = new Bundle();
                startActivity(intent);
            }
        });
    }

    private void loadData() {
        OKHttpUtils.newInstance(getContext()).getAsyncMainData(UrlString.URL_HOME, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                Bean bean = new Gson().fromJson(result, Bean.class);
                mData.addAll(bean.getData().getObject_list());
                //遍历获取单项点击事件跳转的url,并添加到集合
                for (int i = 0; i < bean.getData().getObject_list().size(); i++) {
                    mUrlList.add(bean.getData().getObject_list().get(i).getTarget());
                }
                adapter.notifyDataSetChanged();
            }
        },null);
    }

    private void initToolBar(View view) {
        toolbar = (Toolbar)view.findViewById(R.id.tb_home);
        toolbar.inflateMenu(R.menu.home_menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home_menu:
                        Toast.makeText(getContext(), "卧槽", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
    }
}
